;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F877. This file contains the basic code               *
;   building blocks to build upon.                                    *  
;                                                                     *
;   If interrupts are not used all code presented between the ORG     *
;   0x004 directive and the label main can be removed. In addition    *
;   the variable assignments for 'w_temp' and 'status_temp' can       *
;   be removed.                                                       *                         
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional            *
;   information on the instruction set.                               *
;                                                                     *
;   Template file assembled with MPLAB V4.00 and MPASM V2.20.00.      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************


	list      p=16f877            ; list directive to define processor
	#include <p16f877.inc>        ; processor specific variable definitions
	
	__CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _RC_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF 

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.






;***** VARIABLE DEFINITIONS
w_temp        EQU     0x7E        ; variable used for context saving 
status_temp   EQU     0x7F        ; variable used for context saving








;**********************************************************************
		ORG     0x000             ; processor reset vector
		clrf    PCLATH            ; ensure page bits are cleared
  		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt



main

; remaining code goes here




ERRORLEVEL -302

;Variables del LCD

ADCON1 equ 9fh
PCL equ 02h
STATUS equ 03h

LCD_Datos equ PORTB
LCD_Datos_tris equ TRISB
LCD_Control equ PORTA

PORTA equ 05h
PORTB equ 06h

E equ 3; Puerto A.3
RW equ 2; Puerto A.2
RS equ 1; Puerto A.1

TEMP1 equ 0x030
TEMP equ 0x031
CHAR equ 0x032

LCD_retraso equ d'100'
CBLOCK
MSD
LSD
ContK
ContM
ContN
ContAlterna
ACT
ENDC

; Variables del teclado
KEYB equ 07h
TRISC equ 07h
TECLA equ 22h
CUENTA1 equ 23h

EEADR equ 10dh
EECON1 equ 18ch
EECON2 equ 18dh
EEDATA equ 10ch
INTCON equ 0bh
PIR2 equ 0dh
PIE2 equ 8dh
OPTION_REG equ 181h

; Variables Tiempos

RM equ d'100' ; valor para 0,1 s
RK equ d'249' ; valor para 1 ms

MAXACT equ d'1'; valor del nmero de estados de actividad.Activo o no Activo



org 00h
goto INICIO
org 04h
bcf pir2,4
retfie

;-------------------------TEXTOS EN LCD--------------------------------------

MENU_1         ;pantalla prueba
addwf PCL,f
retlw 'A'  ;Materieal Linea 1
retlw 'B'
retlw 'C'
retlw ' '
retlw 'D'
retlw 'E'
retlw 'F'
retlw b'11000000'
retlw 'P'  ;Material Linea 2
retlw 'R'
retlw 'U'
retlw 'E'
retlw 'B'
retlw 'A'
retlw 'S'
retlw b'10010100'
retlw 'L'  ;Material Linea 3
retlw 'I'
retlw 'N'
retlw 'E'
retlw 'A'
retlw ' '
retlw '3'
retlw b'11010100'
retlw 'F'
retlw 'I'
retlw 'N'
retlw 'A'
retlw 'L'
retlw '.'
retlw '.'
FINAL_MENU_1 retlw 00h



;----------------------------INICIO-------------------------------------------
INICIO clrf status
clrf intcon ; desactiva interrupciones
clrf PIR1 ; sealizacin interrupciones desactivada
BSF STATUS,RP0 ; Banco 1
movlw 0x00
MOVWF OPTION_REG 
clrf pie1 ; desactiva interrupciones externas

bcf STATUS,RP0 ; Banco 0
clrf PORTA
clrf PORTB
clrf PORTC
clrf PORTD
clrf PORT E
clrf T1CON, TMR1ON ; no funciona el timer 1

bsf STATUS,RP0 ; Banco 1
clrf TRISA ; Puerto A como salidas
clrf TRISB; Puerto B como salidas

; PONER AQUI EL RESTO DE PUERTOS?

bsf TRISC,T1OSO ; RC0 como entrada para que funcione el oscilador

bsf PIE1,TMR1IE ; habilita interrupcion TMR1
bsf OPTION_REG,NOT_RBPU ; deshabilita Pullup  en el puerto B
bcf STATUS,rp0 ; banco 0

INICIALIZO_LCD
clrf LCD_Control

ARRANCA_DISPLAY
movlw 0x038 ; interfaz 8 bits y 2 lineas (COMPROBAR SEGUN MODELO DISPLAY)
movwf LCD_Datos
bsf LCD_Control,E ;de 1 a 0 en E habilito la seal
bcf LCD_Control,E 
;-------------------------------RETARDO 1-----------------------------------
RETRASO1_LCD
movlw LCD_retraso
movwf MSD
clrf LSD
LAZO_Retraso1 DECFSZ LSD,f
goto LAZO_Retraso1
DECFSZ MSD,f
GOTO LAZO_Retraso1
;-----------------------------------------------------------------------------

PARTE2  movlw b'00001100' ; DISP_ON
call SEND_CMD
movlw b'00000001' ; CLR_DISP
call SEND_CMD
movlw b'00000110' ; ENTRY MODE SET
call SEND_CMD
movlw b'00000000' ; situar cursor
call SEND_CMD

;-----------------------------------------------------------------------------

movlw 00h
MENSAJE_1 movwf TEMP1
call MENU_1
andlw 0FFh
btfsc STATUS,Z ;chequea si ha terminado
goto OUT_1
call SEND_CHAR
movf TEMP1,w
addlw 1
goto MENSAJE_1

OUT_1
loop
goto loop ;probar mensaje 1 solamente





;---------------------------------SUBRUTINAS LCD-------------------------------
SEND_CHAR
movwf CHAR
call OCUPADO_LCD
movf CHAR,w
movwf LCD_Datos 
bcf LCD_Control,RW
bsf LCD_Control,RS ;Escribir como RW 0 y RS 1
bsf LCD_Control,E
bcf LCD_Control,E
RETURN

SEND_CMD
movwf CHAR
call OCUPADO_LCD
movf CHAR,w
movwf LCD_Datos 
bcf LCD_Control,RW
bcf LCD_Control,RS ;Control como RW 0 y RS 0
bsf LCD_Control,E
bcf LCD_Control,E
RETURN

OCUPADO_LCD
bsf STATUS,rp0 ;Banco 1
movlw 0xff
movwf LCD_Datos_tris
bcf STATUS,rp0 ;Banco 0
bcf LCD_Control,RS
bsf LCD_Control,RW ;Comprobar busy flag como RS 0 y RW 1
bsf LCD_Control,E
bcf LCD_Control,E
movf LCD_Datos,w
movwf TEMP
btfsc TEMP,7
goto OCUPADO_LCD
bcf LCD_Control,RW
bsf STATUS,RP0 ;Banco 1
movlw 0x00
movwf LCD_Datos_tris ; puerto B como salidas otra vez
bcf STATUS,rp0 ;Banco 0
RETURN

END